<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Distributor extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $data = input();
        $where = array();
        $list = db("distribution")->where($where)->paginate( 4, false, ['query'=>request()->param()] );
        $data = $list->items();
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function amout()
    {
        $data = input();
        $where = array();
        $list = db("distribution")->where($where)->paginate( 4, false, ['query'=>request()->param()] );
        $data = $list->items();
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function order()
    {
        $data = input();
        $where = array();
        $list = db("distribution")->where($where)->paginate( 4, false, ['query'=>request()->param()] );
        $data = $list->items();
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function indexes()
    {
        return $this->fetch();
    }
    public function Dsave(Request $request)
    {
        //
        $inputdata=input();
        $res=db("damn")->insert($inputdata);
        if($res){
            $this->success("添加成功",url("Distributor/indexes"));
        }else{
            $this->error("添加失败");
        }
    }
    public function bank()
    {
        $data = input();
        $where = array();
        $list = db("bank")->where($where)->paginate( 4, false, ['query'=>request()->param()] );
        $data = $list->items();
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost())
        {
            $inputdata = input();
            $data = array();
            $data['name'] = $inputdata['name'];
//           $data['status'] = $inputdata['status'];
            $res = db('bank')->insert($data);
            if($res) {
                $this->success('添加成功','Distributor/bank');
            } else {
                $this->error('添加失败');
            }
        } else {
            return $this->fetch();
        }
    }
    public function remove()
    {
        $id= input('id');
        if (db('bank')->where('id',$id)->delete()) {
            $this->success('删除成功',url("Distributor/bank"));
        } else {
            $this->error('删除失败');
        }
    }
    //银行修改
    public function edit()
    {
        $id = input('id');
        if(request()->isPost()) {
            $inputdata = input();
            $data['name'] = $inputdata['name'];

            $bol = db('bank')->where(['id'=>$id])->update($data);
            if($bol) {
                $this->success('编辑成功','Distributor/bank');
            } else {
                $this->success('编辑失败');
            }
        } else {
            $list=db('bank')->where(['id'=>$id])->find();
            $this->assign('list',$list);
            return $this->fetch();
        }
    }
    public function upload()
    {
        $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = "6jDpNaFaqlbu4yaqIbNEVik5Xcu2idiNZKuR_WIy";
        $secretKey = "Pu3AqEZdNXSZk1vNks7hLelsKqXRw1fFcElqJXlZ";
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = "1810p";

        //域名
        $domain = "qv7d7onu9.hb-bkt.clouddn.com";
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }

    }
    public function banks()
    {
        $data = input();
        $where = array();
        $list = db("bank_rules")->where($where)->paginate( 3, false, ['query'=>request()->param()] );
        $data = $list->items();
        $this->assign('data',$data);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function adds()
    {
        if(request()->isPost())
        {
            $inputdata = input();
            $data = array();
            $data['rules'] = $inputdata['rules'];
//           $data['status'] = $inputdata['status'];
            $res = db('bank_rules')->insert($data);
            if($res) {
                $this->success('添加成功','Distributor/banks');
            } else {
                $this->error('添加失败');
            }
        } else {
            return $this->fetch();
        }
    }
    public function removes()
    {
        $id= input('id');
        if (db('bank_rules')->where('id',$id)->delete()) {
            $this->success('删除成功',url("Distributor/banks"));
        } else {
            $this->error('删除失败');
        }
    }
    //银行修改
    public function edits()
    {
        $id = input('id');
        if(request()->isPost()) {
            $inputdata = input();
            $data['rules'] = $inputdata['rules'];

            $bol = db('bank_rules')->where(['id'=>$id])->update($data);
            if($bol) {
                $this->success('编辑成功','Distributor/banks');
            } else {
                $this->success('编辑失败');
            }
        } else {
            $list=db('bank_rules')->where(['id'=>$id])->find();
            $this->assign('list',$list);
            return $this->fetch();
        }
    }
}
